package com.blog.generator;


import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.IoUtil;
import cn.hutool.core.io.resource.ClassPathResource;
import cn.hutool.core.lang.Dict;
import cn.hutool.core.util.ClassUtil;
import cn.hutool.core.util.StrUtil;
import com.blog.utils.FreemarkerUtil;
import freemarker.template.Template;
import lombok.extern.slf4j.Slf4j;

import javax.persistence.Entity;
import java.io.OutputStream;
import java.lang.annotation.Annotation;
import java.util.HashSet;
import java.util.Set;

/**
 * @author: chengqiang
 * @date: 2018/12/6 13:52
 */
@Slf4j
public class JpaGenerator {

    private ClassPathResource classPathResource;
    private Set<Class<?>> classes = new HashSet<>();

    public JpaGenerator() {
        classPathResource = new ClassPathResource("/");
        Set<Class<?>> scanPackage = ClassUtil.scanPackage();
        scanPackage.forEach(e -> {
            Annotation annotation = e.getAnnotation(Entity.class);
            if (annotation != null) {
                classes.add(e);
            }
        });
    }

    public static void main(String[] args) {
        JpaGenerator generator = new JpaGenerator();
        generator.generateService();
//        generator.generateDao();
    }


    private void generateDao() {
        classes.forEach(e -> {
            String entityPackageName = e.getName();
            String entityName = e.getSimpleName();
            String packageName = e.getPackage().getName();
            String daoPackage = packageName.substring(0, packageName.lastIndexOf(".") + 1) + "dao";
            Dict dict = Dict.create()
                    .set("daoPackage", daoPackage)
                    .set("entityPackageName", entityPackageName)
                    .set("entityName", entityName)
                    .set("date", DateUtil.now());
            Template template = FreemarkerUtil.getTemplate("dao.ftl");
            System.out.println(template.getName());
            String daoName = entityName + "Dao.java";
            String resourcePath = classPathResource.getUrl().getFile();
            String daoPath = (resourcePath.replaceAll("target/classes", "src/main/java") + daoPackage).replaceAll("\\.", "/") + "/" + daoName;
            OutputStream out = FileUtil.getOutputStream(daoPath);
            try {
                template.process(dict, IoUtil.getWriter(out, "utf8"));
                log.info("{}生成完毕", daoName);
            } catch (Exception e1) {
                e1.printStackTrace();
            }

        });
    }

    private void generateService() {
        classes.forEach(e -> {
            String entityPackageName = e.getName();
            String entityName = e.getSimpleName();
            String packageName = e.getPackage().getName();
            String daoPackage = packageName.substring(0, packageName.lastIndexOf(".") + 1) + "service";
            String firstLower = StrUtil.lowerFirst(entityName);
            Dict dict = Dict.create()
                    .set("daoPackage", daoPackage)
                    .set("entityPackageName", entityPackageName)
                    .set("entityName", entityName)
                    .set("firstLower", firstLower)
                    .set("date", DateUtil.now());
            Template template = FreemarkerUtil.getTemplate("service.ftl");
            String name = entityName + "Service.java";
            String resourcePath = classPathResource.getUrl().getFile();
            String path = (resourcePath.replaceAll("target/classes", "src/main/java") + daoPackage).replaceAll("\\.", "/") + "/" + name;
            OutputStream out = FileUtil.getOutputStream(path);
            try {
                template.process(dict, IoUtil.getWriter(out, "utf8"));
                log.info("{}生成完毕", name);
            } catch (Exception e1) {
                e1.printStackTrace();
            }
        });
    }

//    private void generateController() {
//        classes.forEach(e -> {
//            String entityPackageName = e.getName();
//            String entityName = e.getSimpleName();
//            String packageName = e.getPackage().getName();
//            String daoPackage = packageName.substring(0, packageName.lastIndexOf(".") + 1) + "service";
//            String firstLower = StrUtil.lowerFirst(entityName);
//            Dict dict = Dict.create()
//                    .set("daoPackage", daoPackage)
//                    .set("entityPackageName", entityPackageName)
//                    .set("entityName", entityName)
//                    .set("firstLower", firstLower)
//                    .set("date", DateUtil.now());
//            Template template = FreemarkerUtil.getTemplate("service.ftl");
//            String name = entityName + "Service.java";
//            String resourcePath = classPathResource.getUrl().getFile();
//            String path = (resourcePath.replaceAll("target/classes", "src/main/java") + daoPackage).replaceAll("\\.", "/") + "/" + name;
//            OutputStream out = FileUtil.getOutputStream(path);
//            try {
//                template.process(dict, IoUtil.getWriter(out, "utf8"));
//                log.info("{}生成完毕", name);
//            } catch (Exception e1) {
//                e1.printStackTrace();
//            }
//        });
//    }
}
